package se.webbzon.boltzmann.game.object.item;

import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.boltzmann.item.AbstractItem;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldeditor.DeflatableWorldObject;
import se.webbzon.oschi01.worldeditor.WorldObjectDeflation;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;
import se.webbzon.oschi01.worldobject.appearance.SimpleAppearance3D;
import se.webbzon.oschi01.worldobject.appearance.XYPlaneAppearance3D;

public class GameItemContainer extends WorldStaticObject implements DeflatableWorldObject {

	// The name of the deflatable world object
	public static final String worldObjectName = "gameItemContainer";
	
	// The abstract environment associated with this game item container
	private final AbstractEnvironment env;
	
	// The shadow of the game item container
	private final GameItemShadow shadow;
	
	// The current item stored in this game item container
	private AbstractItem item;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new empty game item container. **/
	public GameItemContainer(AbstractEnvironment env) {
		this(env,null);
	}
	
	/** Creates a new game item container with a contained item. **/
	public GameItemContainer(AbstractEnvironment env, AbstractItem item) {
		super(	WorldObject.onCreate + WorldObject.onDestroy, 
				new XYPlaneAppearance3D(0, SimpleAppearance3D.TRANSPARENCY, false));
		this.env = env;
		shadow = new GameItemShadow(this);
		
		setItem(item);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Sets the contained item of this game item container. **/
	public void setItem(AbstractItem item) {
		this.item = item;
		SimpleAppearance3D appearance = (SimpleAppearance3D) getAppearance();
		if (item == null) {
			// No item in container
			if (env.isDebugMode())
				appearance.setTextureSet(Resources.getTextureSet("misc_unknown"));
			else
				appearance.setVisible(false);
		} else {
			// Put item in container
			appearance.setTextureSet(item.getTextureSet());
			appearance.setVisible(true);
		}
		
		shadow.setVisible(isVisible());
	}
	
	/** Returns the contained item of this game item container. **/
	public AbstractItem getItem() {
		return item;
	}
	
	@Override public void onCreate(WorldArea world) {
		shadow.launch(world);
	}
	
	@Override public void onDestroy(WorldArea world) {
		shadow.launch(world);
	}
	
	@Override public String getName() {
		return worldObjectName;
	}

	@Override public WorldObjectDeflation getDeflation() {
		return new GameItemContainerDeflation(0,this);
	}

}
